<?php

namespace app\admin\controller;

use think\Controller;
use think\facade\Validate;

class System extends Controller
{
    // 递归菜单
    private function recursion($data, $pid = 0, $level = 0)
    {
        static $arr = [];
        foreach ($data as $k => $v) {
            if ($v['pid'] == $pid) {
                $v['level'] = $level;
                $arr[] = $v;
                $this->recursion($data, $v['id'], $level + 1);
            }
        }
        return $arr;
    }

    // 系统设置
    public function sysInfo()
    {
        if (request()->isAjax()) {
            $data = [
                'id' => input('id'),
                'name' => input('name'),
                'android_version' => input('android_version'),
                'android_url' => input('android_url'),
                'ios_version' => input('ios_version'),
                'ios_url' => input('ios_url'),
                'update_desc' => input('update_desc'),
                'up_ios' => input('up_ios'),
            ];
            $validate = Validate::make([
                'name|网站名称' => 'require',
                'android_version|安卓版本' => 'require',
                'android_url|安卓地址' => 'require',
                'ios_version|苹果版本' => 'require',
                'ios_url|苹果地址' => 'require',
                'update_desc|更新内容' => 'require',
                'up_ios|苹果上架版本' => 'require',
            ]);
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            } else {
                $result = model('System')->isUpdate(true)->save($data);
                if ($result) {
                    $this->success('设置成功');
                } else {
                    $this->error('网络问题，请稍后再试');
                }
            }
        }
        return view();
    }

    // 菜单列表
    public function menuList()
    {
        if (request()->isPost()) {
            $menus = model('AuthRule')->order('sort', 'asc')->select()->toArray();
            $menusInfo = $this->recursion($menus, 0, 0);
            foreach ($menusInfo as $k => $v) {
                $menusInfo[$k]['title'] = str_repeat('|--', $v['level']) . $v['title'];
            }
            $this->result(['data' => $menusInfo, 'count' => count($menusInfo)], 0, '获取分页数据成功', 'json');
        }
        return view();
    }

    // 显示隐藏菜单
    public function showMenu()
    {
        $data = [
            'id' => input('menu_id'),
            'is_show' => input('is_show')
        ];
        $result = model('AuthRule')->isUpdate(true)->save($data);
        if ($result) {
            $this->success('设置成功');
        } else {
            $this->error('网络问题，请稍后再试');
        }
    }

    // 排序菜单
    public function sortMenu()
    {
        $data = [
            'id' => input('menu_id'),
            'sort' => input('sort')
        ];
        $validate = Validate::make([
            'sort|排序' => 'require|number'
        ]);
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        } else {
            $result = model('AuthRule')->isUpdate(true)->save($data);
            if ($result) {
                $this->success('排序成功');
            } else {
                $this->error('网络问题，请稍后再试');
            }
        }
    }

    // 编辑菜单
    public function editMenu()
    {
        if (request()->isAjax()) {
            $data = [
                'id' => input('menu_id'),
                'pid' => input('pid'),
                'title' => input('title'),
                'name' => input('name'),
                'sort' => input('sort'),
                'icon' => input('icon'),
                'is_show' => input('is_show', 0)
            ];
            $validate = Validate::make([
                'title|菜单名称' => 'require|unique:auth_rule',
                'name|菜单路由' => 'require|unique:auth_rule',
                'sort|排序' => 'require|number'
            ]);
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            } else {
                $result = model('AuthRule')->isUpdate(true)->save($data);
                if ($result) {
                    $this->success('编辑成功');
                } else {
                    $this->error('网络问题，请稍后再试');
                }
            }
        }
        $data = [
            'id' => input('menu_id')
        ];
        $oneMenuList = model('AuthRule')->where('pid', 0)->order('sort', 'asc')->select();
        $menuInfo = model('AuthRule')->find($data);
        $viewData = [
            'oneMenuList' => $oneMenuList,
            'menuInfo' => $menuInfo
        ];
        $this->assign($viewData);
        return view();
    }

    // 删除菜单
    public function delMenu()
    {
        $data = [
            'id' => input('menu_id')
        ];
        $sonCount = model('AuthRule')->where(['pid' => $data['id']])->count();
        if ($sonCount) {
            $this->error('有下级菜单，不允许删除！');
        } else {
            $result = model('AuthRule')::destroy($data);
            if ($result) {
                $this->success('删除成功');
            } else {
                $this->error('网络问题，请稍后再试');
            }
        }
    }

    // 添加菜单
    public function addMenu()
    {
        if (request()->isAjax()) {
            $data = [
                'pid' => input('pid'),
                'title' => input('title'),
                'name' => input('name'),
                'sort' => input('sort'),
                'icon' => input('icon'),
                'is_show' => input('is_show', 0)
            ];
            $validate = Validate::make([
                'title|菜单名称' => 'require|unique:auth_rule',
                'name|菜单路由' => 'require|unique:auth_rule',
                'sort|排序' => 'require|number'
            ]);
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            } else {
                $result = model('AuthRule')->save($data);
                if ($result) {
                    $this->success('添加成功');
                } else {
                    $this->error('网络问题，请稍后再试');
                }
            }
        }
        $oneMenuList = model('AuthRule')->where('pid', 0)->order('sort', 'asc')->select();
        $viewData = [
            'oneMenuList' => $oneMenuList
        ];
        $this->assign($viewData);
        return view();
    }
}
